package cn.edu.nuc.onlinestore.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import cn.edu.nuc.onlinestore.Person.ObjectStream;
import cn.edu.nuc.onlinestore.model.Goods;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;

public class AdminAdd extends JFrame {

	private JPanel contentPane;
	private JTextField goodsName;
	private JTextField price;
	private JTextField amount;
    private JFrame jf;
	/**
	 * Launch the application.
	 */
/*	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AdminAdd frame = new AdminAdd();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/

	/**
	 * Create the frame.
	 */
	public AdminAdd(DefaultTableModel model) {
		this.jf=this;
		setTitle("添加商品");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 405);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("商品名称:");
		label.setBounds(35, 57, 67, 15);
		contentPane.add(label);
		
		goodsName = new JTextField();
		goodsName.setBounds(99, 54, 269, 21);
		contentPane.add(goodsName);
		goodsName.setColumns(10);
		
		JLabel label_1 = new JLabel("单价:");
		label_1.setBounds(35, 100, 54, 15);
		contentPane.add(label_1);
		
		price = new JTextField();
		price.setBounds(99, 97, 212, 21);
		contentPane.add(price);
		price.setColumns(10);
		
		JLabel label_2 = new JLabel("单位:元");
		label_2.setBounds(321, 100, 54, 15);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("数量:");
		label_3.setBounds(35, 143, 54, 15);
		contentPane.add(label_3);
		
		amount = new JTextField();
		amount.setBounds(99, 140, 269, 21);
		contentPane.add(amount);
		amount.setColumns(10);
		
		JLabel label_4 = new JLabel("简介:");
		label_4.setBounds(35, 188, 54, 15);
		contentPane.add(label_4);
		
		JTextArea textArea = new JTextArea();
		textArea.setBounds(99, 184, 269, 103);
		contentPane.add(textArea);
		
		JButton button = new JButton("确定添加");
		button.setBounds(275, 310, 93, 23);
		contentPane.add(button);
	    
	
	    
		button.addActionListener(new ActionListener(){
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				File dir=new File("D:/store/goods");
	/*			int FileNum=dir.list().length;
				FileNum++;
				
				
				String[] strFile =dir.list();
		        
				int NO[] = null;
		        int i=0;
				
		        for (String str:strFile) {
					File GoodsName=new File(dir,str);
				   	NO[i]=Integer.parseInt(GoodsName.getName().split(".")[0]);
				   	i++;
				}*/
				
				
				Random random = new Random();

				int maxNo = random.nextInt(89999);

				int  ranNo= maxNo+10000;
				
				String goodsname=goodsName.getText();
			    float goodsprice=Float.parseFloat(price.getText().trim());
			    int goodsamount=Integer.parseInt(amount.getText().trim());
			    String goodsbrief=textArea.getText().trim();
			    
				Goods goods=new Goods(ranNo,goodsname,goodsprice,goodsamount,goodsbrief);
				Boolean bool=ObjectStream.Write("goods/"+ranNo, goods);
				if(bool==true){
				  JOptionPane.showMessageDialog(null,"添加成功"); 
			    } 
				
				model.addRow(new String[]{String.valueOf(ranNo),goodsname,String.valueOf(goodsprice),String.valueOf(goodsamount)});
				jf.dispose();
			}
			
		});
		
		
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				((AdminAdd)e.getSource()).setVisible(false);
			}
		});
		
		
	}
}
